﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.Menu;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domian.Entities.Menu;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System;

namespace B.S.BasicData.Api.Read.Application.Handler.Menu
{
    public class GetBangHandler : IRequestHandler<GetBangCommand, ApiResult<List<GetMenuDTO>>>
    {
        private readonly IBaseRepository<MenuModel> menuRepo;
        private readonly IMapper mapper;

        public GetBangHandler(IBaseRepository<MenuModel>menuRepo,IMapper mapper)
        {
            this.menuRepo = menuRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<GetMenuDTO>>> Handle(GetBangCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<List<GetMenuDTO>>();
            var menu = menuRepo.GetAll().Where(x => x.Pid == 0 && x.IsDel == false).ToList();
            var menudtos = menu.Select(menu => new GetMenuDTO {

                    Id = menu.Id,
                    MenuName = menu.MenuName,
                    Url = menu.Url,
                    Pid = menu.Pid,
                    ParentName = menu.MenuName,
                    CreateBy = menu.CreateBy,
                    CreateData = menu.CreateData,
                    UpdateBy = menu.UpdateBy,
                    UpdateData = menu.UpdateData,
                }).ToList();
            res.Message = "查询成功";
            res.Data = menudtos;
            res.Code = ApiEnum.Ok;
            return Task.FromResult(res);
        }
    }
}
